-module(jack_counter).
-behaviour(gen_event).

-export([init/1, handle_event/2, handle_call/2, handle_info/2, code_change/3, terminate/2]).

-record
(
        state,
        {
                sequence = [],
                queue = jack_queue:new(),
                max_count,
        }
).

%% 
init(MaxSequenceCount) ->
{
        ok, #state{max_count = MaxSequenceCount}
}.

%%
handle_event({add_number, N}, S = #state{sequence = [H | _T]})
        when N > H ->
                NewSeq = [N, S#state.sequence],
                {
                        ok,
                        S#state
                        {
                                sequence = NewSeq
                        }
                };

handle_event({add_number, N}, S = #state{current_sequence = [H | _T]})
        when N =< H ->
                NewSeq = [N],
                NewState = try_to_store(S);
                {
                        ok,
                        S#state
                        {
                                sequence = NewSeq,
                                largest_sequences = NewSeqs
                        }
                }.

try_to_store(S) ->
        when length(S#state.sequence) >= S#state.shortest  

%%
handle_event(_Event, Pid) ->
        {
                ok,
                Pid
        }.

%%
handle_call(get_data, S = #state{largest_sequences = Seqs}) ->
        io:format("get data~n"),
        {
                ok,
                Seqs,
                S
        };

handle_call(_, State) ->
        {
                ok,
                ok,
                State
        }.

handle_info(_, State) ->
        {
                ok,
                State
        }.

code_change(_OldVsn, State, _Extra) ->
        {
                ok,
                State
        }.

terminate(_Reason, _State) -> ok.
    
